package cn.exrick.xboot.modules.oauthUserInfo.controller;

import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.oauthUserInfo.entity.AuthUserInfoEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * @author    : LiYuanzhuo
 * description: 供第三方系统获取授权用户信息
 * create time: 2019/10/14 9:53
 * @return
 */
@RestController
@Slf4j
@RequestMapping(value = "/auth")
public class AuthUserInfoController {

    @Autowired
    private SecurityUtil securityUtil;

    @PersistenceContext
    private EntityManager entityManager;


    @PostMapping("/user/info")
    public Result<Object> userInfo(){
        User u = securityUtil.getCurrUser();
        // 清除持久上下文环境 避免后面语句导致持久化
        entityManager.clear();
        AuthUserInfoEntity authUserInfoEntity = new AuthUserInfoEntity();
        authUserInfoEntity.setUsername(u.getUsername());
        authUserInfoEntity.setIdcardEncryption(u.getIdcardEncryption());
        log.info("第三方系统获取当前登录用户信息{}",authUserInfoEntity.toString());
        return new ResultUtil<>().setData(authUserInfoEntity);
    }
}
